home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MetalComboBoxUI.java < prev    next >
Text File  |  1998-06-30  |  9KB  |  264 lines

  1. /*
  2.  * @(#)MetalComboBoxUI.java    1.10 98/04/21
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.metal;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.plaf.*;
  27. import com.sun.java.swing.border.*;
  28. import com.sun.java.swing.plaf.basic.*;
  29. import java.io.Serializable;
  30. import java.beans.*;
  31.  
  32.  
  33. /**
  34.  * Metal UI for JComboBox
  35.  * <p>
  36.  * Warning: serialized objects of this class will not be compatible with
  37.  * future swing releases.  The current serialization support is appropriate
  38.  * for short term storage or RMI between Swing1.0 applications.  It will
  39.  * not be possible to load serialized Swing1.0 objects with future releases
  40.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  41.  * baseline for the serialized form of Swing objects.
  42.  *
  43.  * @see MetalComboBoxListCellRenderer
  44.  * @see MetalPopupMenuBorder
  45.  * @version 1.10 04/21/98
  46.  * @author Tom Santos
  47.  */
  48. public class MetalComboBoxUI extends BasicComboBoxUI {
  49.  
  50.     public static ComponentUI createUI(JComponent c) {
  51.         return new MetalComboBoxUI();
  52.     }
  53.  
  54.     public void installUI(JComponent c) {
  55.         super.installUI(c);
  56.         comboBox.setRequestFocusEnabled( false );
  57.     }
  58.  
  59.     public void uninstallUI( JComponent c ) {
  60.         super.uninstallUI( c ); 
  61.     }
  62.  
  63.     public void paint(Graphics g, JComponent c) {
  64.     }
  65.  
  66.     protected ComboPopup createPopup() {
  67.         return new MetalComboPopup( comboBox );
  68.     }
  69.  
  70.     protected JButton createArrowButton() {
  71.         JButton button = new MetalComboBoxButton( comboBox,
  72.                                                   new MetalComboBoxIcon(),
  73.                                                   comboBox.isEditable() ? true : false,
  74.                                                   currentValuePane,
  75.                                                   listBox );
  76.         button.setMargin( new Insets( 0, 1, 1, 3 ) );
  77.         return button;
  78.     }
  79.  
  80.     public PropertyChangeListener createPropertyChangeListener() {
  81.         return new MetalPropertyChangeListener();
  82.     }
  83.  
  84.     protected class MetalPropertyChangeListener extends ComboBoxPropertyChangeListener {
  85.         public void propertyChange(PropertyChangeEvent e) {
  86.             super.propertyChange( e );
  87.  
  88.             String propertyName = e.getPropertyName();
  89.  
  90.             if ( propertyName.equals( "editable" ) ) {
  91.                 editablePropertyChanged( e );
  92.             }
  93.         }
  94.     }
  95.  
  96.     protected void editablePropertyChanged( PropertyChangeEvent e ) {
  97.         if ( arrowButton instanceof MetalComboBoxButton ) {
  98.             MetalComboBoxButton button = (MetalComboBoxButton)arrowButton;
  99.             button.setIconOnly( comboBox.isEditable() );
  100.         }
  101.     }
  102.  
  103.     protected LayoutManager createLayoutManager() {
  104.         return new MetalComboBoxLayoutManager();
  105.     }
  106.  
  107.     protected class MetalComboBoxLayoutManager extends ComboBoxLayoutManager {
  108.         public void layoutContainer( Container parent ) {
  109.             layoutComboBox( parent, this );
  110.         }
  111.         public void superLayout( Container parent ) {
  112.             super.layoutContainer( parent );
  113.         }
  114.     }
  115.  
  116.     // This is here because of a bug in the compiler.  When 1.1.6 comes out we
  117.     // should move this into MetalComboBoxLayoutManager.
  118.     public void layoutComboBox( Container parent, MetalComboBoxLayoutManager manager ) {
  119.         if ( comboBox.isEditable() ) {
  120.             manager.superLayout( parent );
  121.         }
  122.         else {
  123.             if ( arrowButton != null ) {
  124.                 Insets insets = comboBox.getInsets();
  125.                 int width = comboBox.getWidth();
  126.                 int height = comboBox.getHeight();
  127.                 arrowButton.setBounds( insets.left, insets.top,
  128.                                        width - (insets.left + insets.right),
  129.                                        height - (insets.top + insets.bottom) );
  130.             }
  131.         }
  132.     }
  133.  
  134.     public boolean isFocusTraversable() {
  135.         return false;
  136.     }
  137.  
  138.     protected void installListeners( JComponent c ) {
  139.         comboBox.addItemListener( itemListener );
  140.         comboBox.addPropertyChangeListener( propertyChangeListener );
  141.     }
  142.  
  143.     protected void removeListeners( JComponent c ) {
  144.         comboBox.removeItemListener( itemListener );
  145.         comboBox.removePropertyChangeListener( propertyChangeListener );
  146.     }
  147.  
  148.     public void configureEditor() {
  149.         super.configureEditor();
  150.         editor.removeKeyListener( popupKeyListener );
  151.     }
  152.  
  153.     public void configureArrowButton() {
  154.         if ( arrowButton != null ) {
  155.             arrowButton.setRequestFocusEnabled( true );
  156.             arrowButton.addKeyListener( keyListener );
  157.             arrowButton.addKeyListener( popupKeyListener );
  158.             arrowButton.addFocusListener( focusListener );
  159.             arrowButton.addMouseListener( popupMouseListener );
  160.             arrowButton.addMouseMotionListener( popupMouseMotionListener );
  161.         }
  162.     }
  163.  
  164.     public void unconfigureArrowButton() {
  165.         if ( arrowButton != null ) {
  166.             super.unconfigureArrowButton();
  167.  
  168.             arrowButton.removeKeyListener( keyListener );
  169.             arrowButton.removeKeyListener( popupKeyListener );
  170.             arrowButton.removeFocusListener( focusListener );
  171.         }
  172.     }
  173.  
  174.     public Dimension getMinimumSize( JComponent c ) {
  175.         Dimension size = null;
  176.  
  177.         if ( !comboBox.isEditable() &&
  178.              arrowButton != null &&
  179.              arrowButton instanceof MetalComboBoxButton ) {
  180.  
  181.             MetalComboBoxButton button = (MetalComboBoxButton)arrowButton;
  182.             Insets buttonInsets = button.getInsets();
  183.             Insets insets = comboBox.getInsets();
  184.  
  185.             size = getDisplaySize();
  186.             size.width += insets.left + insets.right;
  187.             size.width += buttonInsets.left + buttonInsets.right;
  188.             size.width += buttonInsets.right + button.getComboIcon().getIconWidth();
  189.             size.height += insets.top + insets.bottom;
  190.             size.height += buttonInsets.top + buttonInsets.bottom;
  191.         }
  192.         else if ( comboBox.isEditable() &&
  193.           arrowButton != null &&
  194.           editor != null ) {
  195.            size = super.getMinimumSize( c );
  196.            Insets margin = arrowButton.getMargin();
  197.        Insets insets = comboBox.getInsets();
  198.        if ( editor instanceof JComponent ) {
  199.            Insets editorInsets = ((JComponent)editor).getInsets();
  200.            size.height += editorInsets.top + editorInsets.bottom;
  201.        }
  202.        size.height += margin.top + margin.bottom;
  203.            size.height += insets.top + insets.bottom;
  204.        //size.height += 2; // This is to make editable combo boxes the same height as non-editable.
  205.         }
  206.         else {
  207.             size = super.getMinimumSize( c );
  208.         }
  209.         return size;
  210.     }
  211.  
  212.     protected void selectNextPossibleValue() { super.selectNextPossibleValue();}
  213.     protected void selectPreviousPossibleValue() { super.selectPreviousPossibleValue();}
  214.  
  215.     protected void addKeyAccelerators(JComponent comp) {
  216.         super.addKeyAccelerators( comp );
  217.  
  218.         final JComboBox cBox = comboBox;
  219.         final JButton button = arrowButton;
  220.  
  221.         AbstractAction downAction = new AbstractAction() {
  222.             public void actionPerformed(ActionEvent e) {
  223.                 selectNextPossibleValue();
  224.             }
  225.             public boolean isEnabled() {
  226.                 return cBox.isEnabled();
  227.             }
  228.         };
  229.  
  230.         button.registerKeyboardAction( downAction,
  231.                                        KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0),
  232.                                        JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  233.  
  234.         AbstractAction upAction = new AbstractAction() {
  235.             public void actionPerformed(ActionEvent e) {
  236.                 selectPreviousPossibleValue();
  237.             }
  238.             public boolean isEnabled() {
  239.                 return cBox.isEnabled();
  240.             }
  241.         };
  242.  
  243.         button.registerKeyboardAction( upAction,
  244.                                        KeyStroke.getKeyStroke(KeyEvent.VK_UP,0),
  245.                                        JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  246.     }
  247.  
  248.     protected void removeKeyAccelerators(JComponent comp) {
  249.         super.removeKeyAccelerators( comp );
  250.         comboBox.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0));
  251.         comboBox.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0));
  252.     }
  253.  
  254.     protected static class MetalComboPopup extends BasicComboPopup {
  255.         public MetalComboPopup( JComboBox cBox ) {
  256.             super( cBox );
  257.         }
  258.  
  259.         public void delegateFocus( MouseEvent e ) {}
  260.     }
  261. }
  262.  
  263.  
  264.